﻿/*
*AJAX公共方法
*
*CreateBy:   Black
*
*CreateDate: 2009-07-2
*
*调用方法 XmlHttp.sendReq(url,callback);  发送请求( 地址, 回调函数)
/*--------------------------------------------------------------------------*/
var XmlHttp =
    {
        _objPool: [],
        _getInstance: function() {
            for (var i = 0; i < this._objPool.length; i++) {
                if (this._objPool[i].readyState == 0 || this._objPool[i].readyState == 4) {
                    return this._objPool[i];
                }
            }
            // IE5中不支持push方法
            this._objPool[this._objPool.length] = this._createObj();
            return this._objPool[this._objPool.length - 1];
        },
        _createObj: function() {
        
         var objXMLHttp;
        if (!objXMLHttp && window.ActiveXObject) {
                try {
                    objXMLHttp = new ActiveXObject("Msxml2.XMLHTTP.5.0")
                }
                catch (e) {
                    try {
                        objXMLHttp = new ActiveXObject("Msxml2.XMLHTTP.4.0")
                    }
                    catch (e) {
                        try {
                             objXMLHttp = new ActiveXObject("Msxml2.XMLHTTP")
                        }
                        catch (e) {
                            try {
                                objXMLHttp =  new ActiveXObject("Microsoft.XMLHTTP")
                            }
                            catch (e) {

                            }
                        }
                    }
                }
            }
            if (window.XMLHttpRequest) {
                  objXMLHttp = new XMLHttpRequest();
            }
        
        
        
          
            // mozilla某些版本没有readyState属性

            if (objXMLHttp.readyState == null) {

                objXMLHttp.readyState = 0;

                objXMLHttp.addEventListener("load", function() {

                    objXMLHttp.readyState = 4;

                    if (typeof objXMLHttp.onreadystatechange == "function") {

                        objXMLHttp.onreadystatechange();
                    }
                }, false);
            }
            return objXMLHttp;
        },
        // 发送请求( 地址, 回调函数)
        sendReq: function(url, callback) {
            var objXMLHttp = this._getInstance();
            with (objXMLHttp) {

                try {

                    // 加随机数防止缓存

                    if (url.indexOf("?") > 0) {

                        url += "&randnum=" + Math.random();

                    }

                    else {

                        url += "?randnum=" + Math.random();

                    }

                    open("GET", url, true);

                    // 设定请求编码方式

                    setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');

                    send(null);

                    onreadystatechange = function() {

                        if (objXMLHttp.readyState == 4 && (objXMLHttp.status == 200 || objXMLHttp.status == 304)) {

                            callback(objXMLHttp);

                        }

                    }

                }

                catch (e) {

                    alert(e);

                }

            }

        }

    }; 
